﻿--#include "data\functions\TimerFunc.lua" once
--#include "data\config\GlobalLuaConfig.lua" once  								--全局lua脚本
--#include "data\config\GlobalConfig.lua" once  								--全局lua脚本

--#include "data\config\activityconfig\Dart\DartConfig.lua"  once  --基本设置
--#include "data\functions\Activity\Dart\ApplyDartCommon.lua"
--#include "data\functions\Activity\Common\Awards.lua" once 
--#include "data\config\guild\GuildSiegeConfig.lua" once  --行会
--#include "data\config\circle\HonorValue.lua" once
--#include "data\functions\Activity\HistoryTask.lua" once
--#include "data\functions\Activity\PublicTest.lua" once
--#include "data\functions\AchieveEvents.lua" once
--#include "data\config\vip\VipPrivilegesCfg.lua" once
--#include "data\config\activity\StdActivityCfg.lua" once
--#include "data\functions\Quest\QuestCommon.lua" once 				--任务相关方法
--#include "data\config\skill\AutoLearnSkillCfg.lua" once
--#include "data\functions\worldCulture\worldCulture.lua" once 		--世界文明度
--#include "data\functions\Boss\PersonBossCommon.lua" once 			--个人BOSS

--#include "data\functions\Fuben\MaterialFuben\MaterialFuben.lua" once 			-- 材料副本
--#include "data\functions\Fuben\AllDay.lua" once -- 屠龙深渊副本
--#include "data\functions\Fuben\TeamFuben.lua" once							--团队副本
--#include "data\functions\Guild\GuildAdventure.lua" once						--行会宝藏探险

--#include "data\functions\Boss\EquipBossCommon.lua" once 						--装备boss
--#include "data\functions\Activity\DailyActivity\SecretTreasure.lua" once		--龙城秘宝
--#include "data\functions\Activity\DailyActivity\MoZun.lua" once				--魔尊战场活动
--#include "data\functions\Activity\Escort\Escort.lua" once                     --极速押运

--#include "data\functions\Cross\CrossEatChicken.lua" once		--跨服吃鸡
--#include "data\functions\Cross\CrossLeagueMatches.lua" once		--跨服联赛

--//退出游戏触发
function OnPlayerLogout(sysarg)
	OnBossSoulLogout( sysarg )					--兽魂
	OnLogoutTeamFubenAll( sysarg )				--团队副本
	SceneLogoutDispatcher.dispatch( sysarg )	--回调离开某个场景的方法 
	OnEscortLogout(sysarg)						--运镖下线
	CrossLeagueMatchesOffLine(sysarg)
	CrossEatChickenOffLine(sysarg)
end


--//升级触发
function OnPlayerLevelUp(sysarg, oldLevel)
	local var = Actor.getStaticVar(sysarg);
	local d = var.d;
	if not d then var.d = {}; d = var.d; end

	local pLv = Actor.getIntProperty(sysarg,PROP_CREATURE_LEVEL)
	local pCir = Actor.getIntProperty(sysarg,PROP_ACTOR_CIRCLE)
	Quest_OnPlayerLevelUp(sysarg, pLv, oldLevel);
end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

--//死亡触发(死者,杀手)
function OnPlayerDie(sysarg, killer)
	local killerType = Actor.getEntityType(killer)
	local fubenId = Actor.getFubenId(sysarg)
	local nSceneId = Actor.getSceneId(sysarg)
	local var = Actor.getStaticVar(sysarg)
	if not var.days then var.days = {} end
	
	--合服擂台击杀
	OnCombineArenaActorDie( sysarg, killer )

	ServerPKActorDeath(sysarg, killer)  --全服争霸
	--[[
	if nSceneId == 50 then
		if var.days.WLZBSignUpFlag and var.days.WLZBSignUpFlag == 1 then
			var.days.WLZBSignUpFlag = 2		-- 被击败了或主动离开场景
		end
	end
	]]

--[[ 暂时屏蔽
	-- 击杀玩家增加荣誉值
	if killerType == 0 and sysarg ~= killer and Actor.isMatch(killer) == false and Actor.getChallenge(killer) == 0 then
		local kvar = Actor.getStaticVar(killer)
		local killCount = kvar.days.killCount
		if killCount == nil then killCount = 0 end
		
		-- 每人每天前100次击杀能获得荣誉（沙巴克战其间并在皇宫中则不计次数，但荣誉值还是要加）
		if Guild.getSbkWarFlag() == 0 or Actor.getSceneId(killer) ~= 5 then	
			killCount = killCount + 1		
			kvar.days.killCount = killCount		
		end		
			
		-- 因为 Guild.getSbkWarFlag() 可能为0，所以 kvar.days.killCount 这时有可能为 nil
		if killCount <= 100 then		
			local addHonorValue = 0
			local circle = Actor.getIntProperty(sysarg, PROP_ACTOR_CIRCLE)
			if circle > 0 then
				addHonorValue = CircleHonorValueCfg.honorValues[circle]
				if addHonorValue == nil then
					addHonorValue = CircleHonorValueCfg.honorValues[#CircleHonorValueCfg.honorValues]
				end
			else
				local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
				if level >= 80 then
					addHonorValue = CircleHonorValueCfg.circle0
				end
			end
			
			if addHonorValue > 0 then
				local honorValue = Actor.getUIntProperty(killer, PROP_ACTOR_HONOR_VALUE)
				honorValue = honorValue + addHonorValue
				Actor.setUIntProperty(killer, PROP_ACTOR_HONOR_VALUE, honorValue)
				Actor.sendTipmsg(killer, string.format("您杀死了{color;FF00ff00;%s},获得{color;FF00aaff;%d荣誉值},您当前荣誉值{color;FF00aaff;%d}",Actor.getName(sysarg), addHonorValue, honorValue), ttFlyTip)
			end
		end			
	end
--]]

	--[[
	-- 活跃度活动
	if killerType == 0 and sysarg ~= killer and Actor.isMatch(killer) == false and Actor.getChallenge(killer) == 0 then
		OnLivenessPlayerDie(sysarg, killer)
	end

	]]
	
end


--------------------------------------------------------------------------------------------------------------------------------------------------------------------

--[[怪物死亡
不要使用此方法
不同功能的怪物死亡逻辑写到各自的文件中
]]
function OnMonsterByKilled(monster, sysarg, monId, fubenId, sceneId,fuBenPtr)
	--//读取私人变量d
	--[[
	local var = Actor.getStaticVar(sysarg);
	if var == nil then return end -- 玩家登出时这里有可能为 nil
	local d = var.d;
	if not d then d = {} end

	local monsterType = System.getMonsterTypeById(monId)
	
	--统计杀怪总数
	var.killMonsterNum = var.killMonsterNum or 0 
	if var.killMonsterNum < 200000000 then
		var.killMonsterNum = var.killMonsterNum + 1
		Actor.triggerAchieveEvent(sysarg, 97,var.killMonsterNum)
	end

	local pLv = Actor.getIntProperty(sysarg,PROP_CREATURE_LEVEL);
	local mLv = Actor.getIntProperty(monster,PROP_CREATURE_LEVEL);

	if not var.days then var.days = {} end

	if d[3] == 2 then
		if Fuben.getSceneNameById(sceneId) == d[7] then
			d[8] = d[8] + 1;	

			update_quest_target_value(sysarg, 4000, 0, d[8])
				
			if d[8] < d[6] then
				Quest_ShuaXin(sysarg);
			else
				d[3] = 3;
				d[8] = 0;
				local leftCount = 0
				-- 完成降妖除魔次数
				if var.days.xycmCount then
					var.days.xycmCount = var.days.xycmCount + 1
					leftCount = 15 - var.days.xycmCount
				else
					var.days.xycmCount = 1
					leftCount = 15 - var.days.xycmCount
				end
				
				Actor.sendTipmsg(sysarg,"{color;FF00ff00;除魔任务已完成!}", ttFlyTip);
				Actor.addEffect(sysarg,161,6,600,0);
				local str = string.format("今日除魔剩余: %d次",leftCount)
				Actor.messageBox(sysarg,0,0,str,Lang.ScriptTips.zf121.."/WoLongNPC",NULL,NULL)
				OnFinishXiangYaoChuMo(sysarg)
				RandGiveSwingStoneBox(sysarg)
				Actor.openquestarrowsguide(sysarg,"");
			end
		end
	end
	
	if d[13] == 2 then
		if Fuben.getSceneNameById(sceneId) == d[24] then
			d[15] = d[15] + 1;
			
			if d[15] < d[14] then
				Quest_ShuaXin(sysarg);
			else
				d[13] = 3;
				d[15] = 0;
				Quest_ShuaXin(sysarg);
				Actor.sendTipmsg(sysarg,"{color;FF00ff00;除魔任务已完成!}", ttFlyTip);
				Actor.addEffect(sysarg,161,6,600,0);
				--Actor.sendTipmsg(sysarg,"恭喜您完成降妖除魔任务,回去找<卧龙城/M卧龙城:113:122:功德师>的<功德师/M卧龙城:113:122:功德师>交还任务吧. <(x卧龙城:113:122:功德师)>", ttFlyTip);
			end
		end
	end
	]]
--[[
	--支线任务的一些判断 暂时不支持同时两个支线  hwei
	if var.BranchTaskKillMonsterStart and var.BranchTaskKillMonsterStart == 1 then
		if var.BranchTaskKillMonsterScene == nil or var.BranchTaskKillMonsterScene == sceneId then  -- 如果需要判断场景特定怪物的
			if var.BranchTaskillMonsterCount == nil then
				var.BranchTaskillMonsterCount = 0
			end
			if var.BranchTaskillMonsterLimit == nil then
				var.BranchTaskillMonsterCount = var.BranchTaskillMonsterCount + 1
			else
				for i=1,#var.BranchTaskillMonsterLimit do
					if var.BranchTaskillMonsterLimit[i] == monId then
						var.BranchTaskillMonsterCount = var.BranchTaskillMonsterCount + 1
					end
				end
			end
		end
	end
	--]]
	
	--[[镖车被杀,屏蔽，需要时另外做
	local player = Actor.getMonsterOwner(monster)
	local ownerVar = Actor.getStaticVar(player)
	local killname = Actor.getName(sysarg)
	if ownerVar and ownerVar.ybstate ~= nil and ownerVar.ybstate == 1 then
		-- 记录当前位置
		local x, y = Actor.getEntityPosition(monster, 0, 0)		--获取实体X Y坐标
		ownerVar.bcposx = x;
		ownerVar.bcposy = y;
	--	if var.buybx == 1 then
	--		Actor.monsterRealDropItemExp(pKill, sysarg);
	--	end
		--因为是镖车被杀，C++内部会自动删除这个镖车，所以ResetActorDartInfo()
		OnActorDartFailed(player, StdActivityCfg[4].DartSetup.CampDartFailObjDie)
		Quest_ShuaXin(player);
	end
	]]
	
	--[[成就, 82、84已经不在新产品使用
	if monId >= 82 and monId <= 84 then
		local achvKillTimes = GetActorVarAchieveData(sysarg, 1, 4 + monId-82)
		achvKillTimes = achvKillTimes + 1
		SetActorVarAchieveData(sysarg, 1, 4 + monId-82, achvKillTimes)
		Actor.triggerAchieveEvent(sysarg, 111, monId, achvKillTimes)
	end
	]]
	
	--Quest_OnMonsterByKilled(monster, sysarg, monId, fubenId, sceneId,fuBenPtr)
end

--怪物被队友杀死  怪物死亡属于贡献
function OnMonsterByTeammeta(sysarg, monId)
--[[
	local var = Actor.getStaticVar(sysarg);
	if var == nil then return end -- 玩家登出时这里有可能为 nil
	local d = var.d;
	
	local monsterType = System.getMonsterTypeById(monId)
--	if monsterType == 4 then 
	--支线任务的一些判断 暂时不支持同时两个支线  hwei
		if var.BranchTaskKillMonsterStart and var.BranchTaskKillMonsterStart == 1 then
			if var.BranchTaskillMonsterCount == nil then
				var.BranchTaskillMonsterCount = 0
			end
			if var.BranchTaskillMonsterLimit == nil then
				var.BranchTaskillMonsterCount = var.BranchTaskillMonsterCount + 1
			else
				for i=1,#var.BranchTaskillMonsterLimit do
					if var.BranchTaskillMonsterLimit[i] == monId then
						var.BranchTaskillMonsterCount = var.BranchTaskillMonsterCount + 1
						Quest_ShuaXin(sysarg)
					end
				end
			end
		end
	--end
	--]]
end

-------------------------------------------------------------------------------------------------------------------------------------------

--//佩戴装备触发(装备ID, 装备数量)
function OnPlayerDressEquiment(sysarg, equipPtr, equimentId, equimentCount)
	if equimentId <= 0 or equipPtr == nil then
		return;
	end
	
	local nType = Item.getItemProperty(sysarg, equipPtr, Item.ipItemType, 0);
	if Item.itWeaponExtend == nType or Item.itFashionDress == nType then
		OnTakeOnEquimentAutoLearnSkill(sysarg);
	end
end

--//脱下装备触发(装备ID, 装备数量)
function OnPlayerDelEquiment(sysarg)
	OnTakeOffEquimentAutoDelSkill(sysarg);
end


--//商城购买物品触发(购买的物品名字,购买的物品ID,购买物品价格类型,价格)
function OnStoreBuyItem(sysarg, itemId, itemCount)
end

function TestEnterFb(sysarg, fbId)
	--print("--------------TestEnterFb ID="..fbId)
	StartEnterFb(sysarg, fbId)	
end

function ClearFbNum(sysarg, fdId)
	local sVar = Actor.getStaticVar(sysarg)	
	if sVar.fb then
		if fdId then
			sVar.fb[fdId] = {}
		else
			sVar.fb = {}
		end
	end
end

--因为一次可能加几个格子的物品，而且存在叠加的情况，所以提供物品指针是不合理的。
--参数分别代表：玩家指针，物品id,物品数量
function OnAddItemEvent(sysarg,itemId,itemCount)
	--print("OnAddItemEvent, itemId="..itemId..", itemCount="..itemCount)
	if ItemAddDispatcher.dispatch( sysarg, itemId, itemCount ) then 			--有注册事件，并且处理完所注册事件就返回
		return
	end
end

--删除一个物品回调函数
function OnDeleteItemEvent(sysarg,itemId,itemCount)
	if ItemRemoveDispatcher.dispatch( sysarg, itemId, itemCount ) then 			--有注册事件，并且处理完所注册事件就返回
		return
	end
end

--//玩家定时器回调
function OnActorTimer(sysarg, timerId)
	return TimerFunc[timerId](sysarg)
end

--//玩家完成任务
function OnPlayerFinishQuest(sysarg, questId)
end

--------------------------------------------------------------------------------------------------------------

--周四收回王城
function OnRecoveryGuild(nGuildId)
	local pActor = System.getEntityPtrByActorID(Guild.getGuildLeader(nGuildId))
	if pActor then
		local JOB = Actor.getIntProperty(pOActor,PROP_ACTOR_VOCATION)
		local LX = 12
		if JOB == 2 then LX = 16; elseif JOB == 3 then LX = 20; end
		Actor.delBuff(pOActor,LX,206)
	end
end

--踢场景玩家下线
function kickScenePlayer(sceneName,level)
	local nSceneId = System.getSceneIdByName(sceneName)
	local playerList = LuaHelp.getSceneAliveActorListById(nSceneId)		--获取某场景玩家对象列表
	if playerList ~= nil then
		for j = 1, #playerList do
			local player = playerList[j]
			if player and not Actor.checkActorLevel(player,level+1 , 0) then	
				Actor.killActor(player)
			end
		end
	end
end


--系统推荐提示tips框
function SendRecommendTips( sysarg , ntype, nStr)
	local npack = DataPack.allocPacket(sysarg,LogicCmd.miscSys.sysId,LogicCmd.miscSys.sub.sSendRecommendTips)  --申请一个数据包
	if npack == nil then
		print("allocPacket is nil!")
		return
	end
	DataPack.writeByte(npack, ntype)
	DataPack.writeString(npack, nStr)
	DataPack.flush(npack)
end

--[[
检查是否能穿上装备，处理一些C++中不方便判断的逻辑
注意：手工穿戴装备或自动穿戴装备，都会在这里检查
]]
function CanTakeOnEquip(sysarg, pUserItem)
	--[[
	local ItemId = Item.getItemId(pUserItem);
	local ItemName = Item.getItemName(ItemId);
	local Job = Actor.getIntProperty(sysarg,PROP_ACTOR_VOCATION);
	if Job == 1 and string.find(ItemName,"护身") then
		Actor.sendTipmsg(sysarg,"不可佩戴护身特戒", ttFlyTip);
		return false;
	end
	if Job == 2 and string.find(ItemName,"麻痹") then
		Actor.sendTipmsg(sysarg,"不可佩戴麻痹特戒.", ttFlyTip);
		return false;
	end
	]]
	return true;
end

--(不使用)
function OnClearSbkBuff(OldSbkId)
	
	local pOActor = System.getEntityPtrByActorID(OldSbkId)
	if pOActor then
		local nJob = Actor.getIntProperty(pOActor,PROP_ACTOR_VOCATION)
		local nBuffType = 12
		if nJob == 2 then 
			nBuffType = 16; 
		elseif nJob == 3 then 
			nBuffType = 20; 
		end
		Actor.delBuff(pOActor,nBuffType,206)
	end
end

-- gm 开始攻城--(不使用)
function GmStartSbkGuildWar()
	StartSbkGuildWar()
end

-- gm 清除行会归属(不使用)
function GmResetStartSbkTimes(times)
	local var = System.getStaticVar()
	if not var.StartSbkTimes then
		var.StartSbkTimes = 0
	end
	
	var.StartSbkTimes = times
	
	System.setSbkGuildId(0)
end

--(不使用)
function GmCheckSceneSbk()
	Fuben.detectOnlyOneGuild(51)
end

-- gm 结束攻城(不使用)
function GmEndSbkGuildWarOp()
	EndSbkGuildWarOp()
end

--[[
建议不要配置：AcceptQuestCfg
]]
function CheckFuBenQuest_CanFinish(sysarg)	
	local nCurFuBenId = Actor.getFubenId(sysarg)
	if nCurFuBenId <= 0 then		
		return true
	end
		
	if not AcceptQuestCfg then return true end
	local QuestData = AcceptQuestCfg[nCurFuBenId]
	if not QuestData then return true end
		
	-- 飞到该副本交当前任务，然后接下一个任务，完成下一个任务后才能退出副本，所以没有交当前任务或者没有完成下一个任务则不能退出副本
	local canExit = false	
			
	-- 改为在外面交
	if Actor.canFinishQuest(sysarg,QuestData.CurQuestID) or Actor.isFinishQuest(sysarg,QuestData.CurQuestID) then
		canExit = true
	end
	
	--[[
	if Actor.isFinishQuest(sysarg,QuestData.CurQuestID) then		
		-- 如果子任务已经交了或者子任务完成了（即可以交了）	
		if Actor.isFinishQuest(sysarg,QuestData.SonQuestID) or Actor.canFinishQuest(sysarg,QuestData.SonQuestID) then
			canExit = true
		end
	else
		--血域殿、虹魔殿、模拟皇宫[副本]，有玩家反映传到了这个副本但是没接这个任务以至于出不去，所以这里自动帮他接一下
		--if (QuestData.CurQuestID == 59 or QuestData.CurQuestID == 35 or QuestData.CurQuestID == 62) and not Actor.hasQuest(sysarg, QuestData.CurQuestID) then
		if (not Actor.hasQuest(sysarg, QuestData.CurQuestID)) then -- 自动帮他接一下
			Actor.addQuest(sysarg, QuestData.CurQuestID)
		end
	end
	--]]

	if not canExit then
		Actor.sendTipmsg(sysarg,"请完成任务后再来传送", ttFlyTip);
	end
	
	return canExit
end
--[[
function OnChangeSBKLeader(oldLeaderId,newLeaderId)
	
	local sysarg = System.getEntityPtrByActorID(oldLeaderId)
	local nCount = Actor.getItemCount(sysarg,GuildSiegeConfig.SbkLeaderWeapons,0,0,-1,-1,nil,true)
	if nCount > 0 then
		Actor.removeItem(sysarg,GuildSiegeConfig.SbkLeaderWeapons,1,-1,-1,-1,"帮主禅让删除王城城主的专属武器",2,-1,true)
	end
	
	local sVar = System.getStaticVar()
	if not sVar.LeaderPrivilege then
		sVar.LeaderPrivilege = {}
	end
	sVar.LeaderPrivilege[1] = nil
end
]]

--[[穿上装备触发学习技能
AutoLearnSkillCfg,lua配置
]]
function OnTakeOnEquimentAutoLearnSkill(sysarg)
	--[[以圣器最小的阶数获得技能
	local cfg = AutoLearnSkillCfg.hallowsSkill;
	local job = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
	local skillId = cfg.skillIds[job];
	if not skillId then
		return;
	end
	
	local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
	local weaponPtr = Item.getEquipByType(sysarg, Item.itWeaponExtend)
	local dressPtr = Item.getEquipByType(sysarg, Item.itFashionDress)
	if not weaponPtr or not dressPtr then
		Actor.delSkill(sysarg, skillId);
		return;
	end
	
	-- 装备性别索引
	local sexIdx = 0;
	if sex == 0 then
		sexIdx = 2;
	else
		sexIdx = 3;
	end
	
	local weaponid = Item.getItemProperty( sysarg, weaponPtr, Item.ipItemID, 0)
	local dressid = Item.getItemProperty( sysarg, dressPtr, Item.ipItemID, 0)
	
	local weaponIdx = 0;
	local dressIdx = 0;
	local skillLevel = 0;
	
	for i, tab in ipairs(cfg.equipIds) do
		if weaponid == tab[1] then
			weaponIdx = i;
		end
		
		if dressid == tab[sexIdx] then
			dressIdx = i;
		end
	end
	
	if weaponIdx <= 0 or dressIdx <= 0 then
		return;
	end
	
	if weaponIdx == dressIdx then
		skillLevel = weaponIdx;
	elseif weaponIdx < dressIdx then
		skillLevel = weaponIdx;
	else
		skillLevel = dressIdx;
	end

	-- 如果没有学习返回0
	local currSkillLevel = Actor.getSkillLevel(sysarg, sillId)
	if currSkillLevel == dressIdx then
		return;
	end
	
	Actor.startLearnSkill(sysarg, skillId, skillLevel)
	]]
end

--[[脱下装备触发删除技能
AutoLearnSkillCfg,lua配置
]]
function OnTakeOffEquimentAutoDelSkill(sysarg)
	--[[以圣器最小的阶数获得技能
	local cfg = AutoLearnSkillCfg.hallowsSkill;
	local job = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
	local skillId = cfg.skillIds[job];
	if not skillId then
		return;
	end
	
	local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
	local weaponPtr = Item.getEquipByType(sysarg, Item.itWeaponExtend)
	local dressPtr = Item.getEquipByType(sysarg, Item.itFashionDress)
	if weaponPtr ~= nil and dressPtr ~= nil then
		return;
	end
	
	Actor.delSkill(sysarg, skillId);
	]]
end


--@gfunc OpenCompensateGifts
function OpenCompensateGifts()
	local dVar = System.getDyanmicVar()
	if dVar.openCompensate == nil or dVar.openCompensate ~= 1 then
		dVar.openCompensate = 1
	end
	
	print("打开补偿成功,第2天自动关闭")
end

-- 26 28协议，下发开服第几天 ，合服第几天 ，合服时间  开服时间
function SendOpenServerDays(sysarg)
	local netPack = DataPack.allocPacket(sysarg, 26, 28)
	if not netPack then
		return false;
	end


	local nOpenDays = System.getDaysSinceOpenServer()
	local nCombineDays = System.getDaysSinceCombineServer()

	DataPack.writeInt(netPack, nOpenDays)
	DataPack.writeInt(netPack, nCombineDays)

	local nCombineTime = System.getServerCombineTime()
	DataPack.writeInt(netPack, nCombineTime)

	local nOpenTime = System.getServerOpenTime()
	DataPack.writeInt(netPack, nOpenTime)

	local loginDays = Actor.getLoginDays( sysarg )			--如果在线跨天，一定是先登陆天数+1，再发送
	DataPack.writeInt(netPack, loginDays)

	local worldLevel = System.getCultureLevel()
	DataPack.writeShort(netPack, worldLevel)

	local nCreateDays = Actor.getDaysSinceCreateRole(sysarg)
	DataPack.writeInt(netPack, nCreateDays)

	local worldCircle = System.getCultureCircle()
	DataPack.writeByte(netPack, worldCircle)

	DataPack.flush(netPack);
end

--离线清理数据
function ClearOfflineActorsData()
	System.clearOfflineActorsData()
end


--设置排行榜第一名雕像
function SetRankNpcStatueName(rankType, name, sex)
	local entityName = Lang.EntityName.n00075
	local entityId = 75
	if rankType == 2 then
		entityName = Lang.EntityName.n00076
		entityId = 76
	elseif rankType == 3 then
		entityName = Lang.EntityName.n00077
		entityId = 77
	else
		rankType = 1
	end
	local pNpc = Fuben.getNpcPtrById(GuildSiegeConfig.RankStatusScene,entityId)
	if pNpc then
		if name and name ~= "" then
			Actor.changeShowName(pNpc,10, name)
			Actor.changeEntityModel(pNpc,10, GuildSiegeConfig.RankStatusModelList[((rankType-1)*2)+sex+1])
			return
		end	
		Actor.changeShowName(pNpc,10, entityName)
		Actor.changeEntityModel(pNpc,10, GuildSiegeConfig.RankStatusModelList[((rankType-1)*2)+1])
	end
end


function OnKillActorDropLuck(sysarg, killerPtr)
	if not killerPtr then
		return
	end
	if not sysarg then
		return
	end
	if Actor.GetNameColorData(sysarg) > 1 then
		return
	end
	if Actor.hasMapAreaAttri( killerPtr, aaKillNotDropLucky ) then
		return 
	end
	local weaponPtr = Item.getEquipByType(killerPtr, Item.itWeapon)
	if not weaponPtr then	
		return
	end
	local luckVal = Item.getItemProperty(killerPtr, weaponPtr, Item.ipItemLuck, 0)	
	local killerLevel = Actor.getActorTotalLevel(killerPtr)
	local sysargLevel = Actor.getActorTotalLevel(sysarg)
	local level = killerLevel - sysargLevel
	local config = GetElemByCond(GlobalLuaConfig.KillDropLucky, level)
	if not config then
		config = GlobalLuaConfig.KillDropLucky[#GlobalLuaConfig.KillDropLucky]
	end
	local randVal = math.random(10000)
	if randVal <= config.dropRate then
		luckVal = luckVal - config.dropValue
		if luckVal > GlobalConfig.maxLuckyValue then
			luckVal = GlobalConfig.maxLuckyValue
		elseif luckVal < GlobalConfig.minCurseValue then
			luckVal = GlobalConfig.minCurseValue
		end
		Item.setItemProperty(killerPtr, weaponPtr, Item.ipItemLuck,  luckVal)
		Actor.sendTipmsg(killerPtr, Lang.ScriptTips.KillDropLucky001, ttFlyTip)
	end
end

--[[离线副本保存handler
注意：新开发功能，使用GlobalLuaConfig.reloginFubenList[curFubenId] 方式判断是否需要保存副本handle，
]]
function DroplineSaveFubenHandler(sysarg)
	local sVar 			= Actor.getStaticVar(sysarg)
	local fubenPtr 		= Actor.getFubenPrt(sysarg)
	local fubenHandle  	= Fuben.getFubenHandle(fubenPtr)
	local curFubenId 	= Fuben.getFuBenId(fubenPtr)
	local actorId 		= Actor.getActorId(sysarg)
	if GlobalLuaConfig.reloginFubenList[curFubenId] then 		--[新功能走这里]需要掉线重登的副本
		sVar.saveFubenHandle = fubenHandle
		--Fuben.setOwnerId(fubenPtr, actorId)					--这里不需要，因为可能是多人副本，并且应该在初始进入时设置ownerId
	else 		--这里用于兼容旧功能
		for idx, fubenLst in ipairs(GlobalLuaConfig.needSaveFubenLst) do
			for ii, fubenId in ipairs(fubenLst) do
				if fubenId == curFubenId then
					sVar.saveFubenHandle = fubenHandle
					Fuben.setOwnerId(fubenPtr, actorId)
					break
				end
			end
		end
	end
end

--上线触发回到副本
function OnLoginDropLineRebackToFuben(sysarg)
	--print("OnLoginDropLineRebackToFuben...")
	local sVar = Actor.getStaticVar(sysarg)
	if sVar.saveFubenHandle then
		local fubenPtr = Fuben.getFubenPtr(sVar.saveFubenHandle) --因为重用，此时这个fubenPtr可能已经不是原先那个下线的副本了
		if not fubenPtr then 
			sVar.saveFubenHandle = nil
			return 
		end

		--[[
		下面的idx，与SetNeedReloginFuben()的设置的idx对应
		GlobalLuaConfig.reloginFuben.xxxxx
		]]
		local reloginFubenId 	= Fuben.getFuBenId(fubenPtr) 				--需要重登陆的副本
		local reloginIdx 		= GlobalLuaConfig.reloginFubenList[reloginFubenId]
		--print("OnLoginDropLineRebackToFuben, reloginFubenId="..reloginFubenId..", reloginIdx="..reloginIdx)
		if reloginIdx and reloginIdx > 0 then
			ProcLoginCommonFuben( sysarg, fubenPtr, reloginIdx )
		else 			--这里是为了兼容旧功能
			local actorId = Actor.getActorId(sysarg)
			local fubenActorId = Fuben.getOwnerId(fubenPtr)
			if fubenActorId ~= actorId then
				sVar.saveFubenHandle = nil
				return
			end
		
			for idx, fubenLst in ipairs(GlobalLuaConfig.needSaveFubenLst) do
				for ii, fubenId in ipairs(fubenLst) do
					if fubenId == reloginFubenId then
						--if idx == 1 then  			--材料副本
						--	OnMaterialFubenLogin(sysarg, fubenPtr)
						if idx == 2 then  		--屠龙深渊
							OnAllDayLogin(sysarg, fubenPtr)
						elseif idx == 4 then 		--石墓烧猪
							ProcLoginStoneTombPig( sysarg, fubenPtr )
						elseif idx == 5 then 		--魔域迷城
							ProcLoginMagicCity( sysarg, fubenPtr )
						elseif idx == 6 then 		--血域试炼
							ProcLoginSpaceTrial( sysarg, fubenPtr )
						elseif idx == 7 then 		--装备boss
							ProcLoginEquipBoss( sysarg, fubenPtr  )
							break
						end
					end
				end
			end
		end
	end
	sVar.saveFubenHandle = nil
end

function verifyCheckLoginGame(sysarg, verifyCheckValue)
	local level = Actor.getActorTotalLevel(sysarg)
	if level ~= 1 then
		return 
	end

	if verifyCheckValue and verifyCheckValue == 10 then
		local myLevel = System.getRandomNumber(GlobalLuaConfig.verifyCheckLevRange[1], GlobalLuaConfig.verifyCheckLevRange[2])
		local ranNum = System.getRandomNumber(#GlobalLuaConfig.verifyCheckSceneLst) + 1
		local myScene = GlobalLuaConfig.verifyCheckSceneLst[ranNum]

		local nActorJob = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
        local nActorSex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)


		local ranNum2 = System.getRandomNumber(#GlobalLuaConfig.verifyCheckEquipLst) + 1
		local myEquip = GlobalLuaConfig.verifyCheckEquipLst[ranNum2]
		Actor.setActorCircle(sysarg, myEquip.circle, myLevel)
		Actor.enterScene( sysarg, unpack(myScene))

		for _, award in ipairs(myEquip.awards) do
			local myAward = FilterCommonAwardByCond( award, nActorSex, nActorJob )
			if myAward then
				for idx = 1, myAward.count do
					local newEquipPtr = Item.createItem(myAward.id, 1, 0, 0, myAward.bind or 0)
					if newEquipPtr then
						local addCount = Item.addItem( sysarg, newEquipPtr, Lang.LogTips.VerifyCheckEquip, LogIds.VerifyCheckEquipLogId, 1)
						if addCount >= 1 then
							Item.equipItem( sysarg, newEquipPtr )	
						end
					end
				end 
			end
		end
	end
end


function OnRankSendWeekRewards()
	for idx = enVocWarrior, enVocWizard do
		local cfg = GlobalLuaConfig.RankingPlayerWeekReward[idx]
		local list = LuaHelp.getActorTopTenByVocation(idx, cfg.count)
		local len = 0
		if list then
			len = #list
		end
		if len ~= 0 and cfg.count == #cfg.awards then
			if len > cfg.count then
				len = cfg.count
			end
			for i = 1, len do
				local playerId = Ranking.get_ranking_item_id(list[i])
				local playerLevel = Ranking.get_ranking_item_level(list[i])
				if playerLevel >= cfg.needAwardLevel then
					local rewards = cfg.awards[i]
					local title = Lang.ScriptTips.RankingTips001
					local content = string.format(Lang.ScriptTips.RankingTips002, i)
					Awards.GiveToMailById(playerId, rewards, title, content, LogIds.RankingWeekAwardLogId, Lang.LogTips.RankingWeekAward)
				end
			end
		end
	end
end
